/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entities.InforCompany;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author MrChinh
 */
public class ListInforCompany {

    ConnectDB myConn = new ConnectDB();
    Connection conn;
    
    public List<InforCompany> getListInforCompany(){
        List<InforCompany> listIP = new ArrayList<InforCompany>();
        try {
            conn = myConn.getConnect();
            Statement stmt  = conn.createStatement();
            String sql = "SELECT * FROM InforCompany";
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()){
                InforCompany ifc = new InforCompany();
                ifc.setCompanyID(rs.getInt(1));
                ifc.setCompanyName(rs.getString(2));
                ifc.setCompanyOverview(rs.getString(3));
                ifc.setCompanyAddress(rs.getString(4));
                ifc.setCompanyWebsite(rs.getString(5));
                listIP.add(ifc);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listIP;
    }
    
    public InforCompany getCompanyByVacancyId(String vId){
        InforCompany cp = new InforCompany();
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("SELECT CP.* ");
        sqlBuilder.append("FROM ");
        sqlBuilder.append("InforCompany CP ");
        sqlBuilder.append("INNER JOIN Vacancy V on CP.CompanyID = V.CompanyID ");
        sqlBuilder.append("WHERE V.VacancyID = '");
        sqlBuilder.append(vId);
        sqlBuilder.append("'");
        String sql = sqlBuilder.toString();
        try {
            conn = myConn.getConnect();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()){
                cp.setCompanyID(rs.getInt("CompanyID"));
                cp.setCompanyName(rs.getString("CompanyName"));
                cp.setCompanyOverview(rs.getString("CompanyOverview"));
                cp.setCompanyAddress(rs.getString("CompanyAddress"));
                cp.setCompanyWebsite(rs.getString("CompanyWebsite"));
                cp.setImage(rs.getString("Image"));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return cp;
    }
}
